package org.sbbs.admin.mbean.sysparam;

import jakarta.faces.application.FacesMessage;
import static jakarta.faces.application.FacesMessage.SEVERITY_WARN;
import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.validator.ValidatorException;
import jakarta.inject.Named;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.omnifaces.cdi.ViewScoped;
import org.sbbs.admin.dao.SysParameterRepo;
import org.sbbs.admin.entity.SysParameter;
import org.sbbs.admin.manager.SysParameterManager;
import org.sbbs.base.jsf.BaseTableMBean;
import static org.sbbs.base.jsf.BaseTableMBean.addDetailMessage;

/**
 *
 * @author jenkins
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Named("sysParamMB")
//@Component("sysParamMB")
@ViewScoped
//@SessionScoped
public class SysParaMbean extends BaseTableMBean<SysParameter, Long, SysParameterRepo, SysParameterManager> {

    private static final long serialVersionUID = -8170966700137807744L;

    public SysParaMbean(SysParameterManager m) {
        super(m);
    }

    @Override
    public SysParameter createNewObj() {
        return new SysParameter();
    }

//    @Override
//    public SysParameter getEditObj() {
//        return super.getEditObj(); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
//    }
    public void showMessage(Object msg) {
        addDetailMessage("fdc", SEVERITY_WARN);
    }

    public void keyUniqValidate(FacesContext context, UIComponent component, Object value) {

        String key = (String) value;
        SysParameter sp = this.getManager().getByKey(key);

        if (sp != null && !sp.getId().equals(this.getEditObj().getId())) {
            FacesMessage fms = new FacesMessage(this.getSma().getMessage("sys.parameter.key.uniq"));
            fms.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(fms);
        }
    }
}
